<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  
  <meta name="generator" content="Hugo 0.98.0" />

  
  <meta name="description" content="走在通往幸福的路上">
  

  
  <link rel="apple-touch-icon" sizes="180x180" href="https://blog.v5u.win/apple-touch-icon.png">

  
  <link rel="icon" type="image/png" sizes="32x32" href="https://blog.v5u.win/favicon-32x32.png">

  
  <link rel="icon" type="image/png" sizes="16x16" href="https://blog.v5u.win/favicon-16x16.png">

  
  <link rel="manifest" href="https://blog.v5u.win/site.webmanifest">

  
  <link rel="mask-icon" href="https://blog.v5u.win/safari-pinned-tab.svg" color="">

  <meta name="msapplication-TileColor" content="">

  <meta name="theme-color" content="">

  
  <link rel="stylesheet" href="https://blog.v5u.win/css/bootstrap.min.css" />

  
  <title>iOS键盘高度的官方获取方法 | 为吾优</title>
  

  <style>
body {
  min-width: 300px;
}

.custom-navbar {
  margin-bottom: 1em;
  height: 60px;
}

.custom-navbar a {
  display: inline-block; 
  padding: 18px 0;
  margin-right: 1em; 
  font-weight: bold; 
}

.custom-navbar a:hover,
.custom-navbar a:focus {
  text-decoration: none; 
}

@media print {
  .custom-navbar {
    display: none;
  }
}

article {
  padding-bottom: 1em;
}

img {
  max-width: 100%;
}


body {
  background-color: #fff;
}



body {
  color: #212529;
}



a {
  color: #007bff;
}



a:hover,
a:focus {
  color: #0056b3;
}



.custom-navbar {
  background-color: #212529;
}



.custom-navbar a {
  color: rgba(255,255,255,.75);
}



.custom-navbar a:hover,
.custom-navbar a:focus {
  color: rgba(255,255,255,1);
}



.container {
  max-width: 800px;
}





</style>
</head>

<body>
  <nav class="custom-navbar">
  <div class="container">
    
    <a href="/">文章</a>
    
    <a href="/tags/">标签</a>
    
    <a href="/about/">关于</a>
    
    <a href="/index.xml">RSS</a>
    
  </div>
</nav>
  
  <div class="container">
    <article>
      <h1>iOS键盘高度的官方获取方法</h1>
<p>处理键盘事件的正确方法是这样的：（包括获取键盘的高度以及键盘弹出和消失动画的时间）</p>
<p>1）在要使用键盘的视图控制器中，接收键盘事件的通知：</p>
<!-- raw HTML omitted -->
<pre><code>        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

       [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];`

`        // 键盘高度变化通知，ios5.0新增的
#ifdef __IPHONE_5_0
        float version = [[[UIDevice currentDevice] systemVersion] floatValue];
        if (version &amp;gt;= 5.0) {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
        }
#endif`&lt;/pre&gt;
&lt;div class=&quot;cnblogs_code_toolbar&quot;&gt;&lt;span class=&quot;cnblogs_code_copy&quot;&gt;&lt;a title=&quot;复制代码&quot;&gt;　　2）然后添加键盘事件的处理代码：&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
获取到当前keyboard的高度以及动画时间，然后对视图进行对应的操作即可。
&lt;div class=&quot;cnblogs_code&quot;&gt;
&lt;div class=&quot;cnblogs_code_toolbar&quot;&gt;
&lt;pre&gt;`#pragma mark - #pragma mark Responding to keyboard events
- (void)keyboardWillShow:(NSNotification *)notification {
/* Reduce the size of the text view so that it's not obscured by the keyboard. Animate the resize so that it's in sync with the appearance of the keyboard. */

 NSDictionary *userInfo = [notification userInfo];

// Get the origin of the keyboard when it's displayed.
 NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

// Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.

 CGRect keyboardRect = [aValue CGRectValue];

// Get the duration of the animation.

 NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
 NSTimeInterval animationDuration;
 [animationDurationValue getValue:&amp;amp;animationDuration];

// Animate the resize of the text view's frame in sync with the keyboard's appearance.
 [self moveInputBarWithKeyboardHeight:keyboardRect.size.height withDuration:animationDuration];
}

- (void)keyboardWillHide:(NSNotification *)notification {

 NSDictionary* userInfo = [notification userInfo];

/* Restore the size of the text view (fill self's view). Animate the resize so that it's in sync with the disappearance of the keyboard. */

 NSValue *animationDurationValue = [userInfo  objectForKey:UIKeyboardAnimationDurationUserInfoKey];
 NSTimeInterval animationDuration;
 [animationDurationValue getValue:&amp;amp;animationDuration];
 [self moveInputBarWithKeyboardHeight:0.0 withDuration:animationDuration];
}
</code></pre>
<!-- raw HTML omitted -->
<p>ios5隐藏功能分享——“字典”功能（英英字典）：</p>
<p>在任何输入框中选中一个英文单词，此时会有选择项“复制”，“删除”&hellip;等，还有一个向右的箭头，点击这个向右的箭头后，就会出现“定义”选项，点击这个“定义”按钮即会弹出这个英语单词的英文解释。</p>
<p>原文链接</p>
<p><a href="http://www.cnblogs.com/zhulin/archive/2011/10/15/2213687.html">http://www.cnblogs.com/zhulin/archive/2011/10/15/2213687.html</a></p>

    </article>
  </div>

  
  
  

  
</body>

</html>